<?php

class Account_add  extends AdminManage
{
    
    
    
	/**
	 * 初始化
	 */
	public function _init()
	{
        #权限 lee
        $this->checkAuthority(); 
        
	}

	/**
	 * 默认动作
	 */
	protected function _default()
	{
		 #添加用户操作
		 if ($_SERVER["REQUEST_METHOD"] == "POST"){ 
		 
		    $userid = $this->getParam('uid');
			
			#判断是否编辑
			$username = $this->getParam('username');
			if(empty($userid)){
				#判断用户是否存在
				
				$sql = "select count(1) CNT from system where username = '".$username."'"; 
				if(DB::select($sql,-1) > 0){
					$this->alertBack('用户名"'.$username.'"已存在，请重新输入！');
				}
			}
			#判断密码是否一致
            $pwd = $this->getParam('pwd');
            $repwd = $this->getParam('repwd');
     
            if($pwd && $repwd){
                if($pwd !== $repwd){
                    $this->alertBack('两次密码输入不一致，请重新输入！');
                }
            }else{
                $this->alertBack('账号密码不能为空，请重新输入！');
            }
			
			#判断姓名是否正常
            $fullname = $this->getParam('fullname');
            if(!$fullname){
                $this->alertBack('姓名不能为空');
            }
			#补充信息处理
			$jointime = $this->getParam('jointime');
            if($jointime){
                $jointime = strtotime($jointime);
            }
			
			$sex = 1;
			
			$mobile = $this->getParam('mobile');
			$tel = $this->getParam('tel');
			$qq = $this->getParam('qq');
			$sex = $this->getParam('sex');
			$remark = $this->getParam('remark');
			$email = $this->getParam('email');
			
			$crypt = new Crypt();//密码加密
			$userPassword = $crypt->encode($pwd);
            
            $addDB['username']= $username;
            $addDB['fullName'] = $fullname;
            $addDB['password'] = $userPassword;
			$addDB['mobile'] = $mobile;
			$addDB['tel'] = $tel;
			$addDB['email'] = $email;
			$addDB['qq'] = $qq;
			$addDB['remark'] = $remark;
			$addDB['sex'] = $sex;
			$addDB['state'] = $this->getParam('state');
            $addDB['dateline'] = time();
			if(empty($userid)){
			   $addDB['firstTime'] =  time();
			}
			$addDB['joinTime'] = $jointime;
			
			#菜单设置
            $checkArr = $this->getParam('check');
            if($checkArr){
                $addDB['menuId'] = implode(",",$checkArr);
            }else{
				$addDB['menuId'] = '';
			}
			
			#部门
			$depart = $this->getParam('depart');
			$addDB['group_id'] = intval($depart);

			#logo处理
			if(is_array($_REQUEST['logo-up_imgfiles'])&&count($_REQUEST['logo-up_imgfiles']>0)){
				$addDB['logo']=join(',',$_REQUEST['logo-up_imgfiles']);
			}
 
            if($addDB['username'] && $addDB['fullName'] && $addDB['password'] ){
				
				if(empty($userid)){
					if(DB::insert('system',$addDB)){
                        $this->alertGoto('添加管理员成功！','/admins/account_info.html');
					}else{
						$this->alertBack('添加失败，请重试！');
					}
				}else{
					if(DB::update('system','id='.$userid,$addDB)){
                        $this->alertGoto('修改管理员成功！','/admins/account_info.html');
					}else{
						$this->alertBack('修改失败，请重试！');
					}
				}
                
            }else{
                $this->alertBack('请填写完整选项！');
            }
        }
		
		
		#判断是否编辑
		$uid = $this->getParam('id');
		$this->assign('uid',$uid);
		if(!empty($uid) && is_numeric($uid)){
			#获取帐号信息
			$sqlUser="select id,username,password, fullName as name,state,dateline,firstTime,joinTime,tel,qq,email,logo,mobile,sex,group_id,remark,menuId from system where id = ".$uid.' limit 1';
            $arrUser = DB::select($sqlUser);
			if($arrUser){
				$arrUser = $arrUser[0];
				#密码
				$crypt = new Crypt();//密码加密
			    $userpwd = $crypt->decode($arrUser['PASSWORD']);
				$arrUser['OLDPASS'] = $userpwd;
				#帐号加入时间
				$arrUser['JOINTIME'] = date('Y-m-d',$arrUser['JOINTIME']?$arrUser['JOINTIME']:time());
				$this->assign('arrUser',$arrUser);
				#帐号权限
				$menuArr = explode(",",$arrUser['MENUID']);
				foreach ($menuArr as $k=>$v){
					$newmenuArr[$v] = $v;    
				}
				$this->assign('menuIdList',$newmenuArr);
				//图片
			    $oldimg = '';
				if(!empty($arrUser['LOGO'])) {
	
					$arrUser['LOGO']=explode(',',$arrUser['LOGO']);//拆分图片路径
	
					foreach($arrUser['LOGO']   as $k=>$v   ){
						$oldimg['list'][$k]['filepath'] = Func::imageSrcHandle($v,'z','85*85','1');
						$oldimg['list'][$k]['id'] = $k;
					}
	
					$oldimg['img_cnt_limit'] = 1;
					$imgjs = getUploadImgCode('logo-up','banner',$oldimg);
					$this->assign('imgjs',$imgjs);
	
				}
			
			}
		}

        #获取到部门信息
		$arrDepartment = $this->get_department_info();
		$this->assign('arrDepartment',$arrDepartment);
		
		#获取到菜单信息
		$objAdmins = $this->getModel('admins');
		$arrMenuParentList =$objAdmins->getMenuData();
		if($arrMenuParentList){
			$arrMenuChildList = array();
			$arrCheckChild = array();
			
			foreach($arrMenuParentList as $value)
			{
				//if(strlen($value['MENUID']) > 3){
				  if(!in_array($value['MENUID'],$arrCheckChild)){
					#获取子菜单
					$arrMenuChildList[substr($value['MENUID'],0,3)][] = $value;
					array_push($arrCheckChild,$value['MENUID']);
				  }
				  $pMenuId[] = substr($value['MENUID'],0,3);
			  // }
			}
			#获取父菜单
			$strpMenuId = implode(',',array_unique($pMenuId));
			$arrMenuList =$objAdmins->getMenuById($strpMenuId,1);
		    $this->assign('arrMenuChildList',$arrMenuChildList);
		    $this->assign('arrMenuParentList',$arrMenuList);
		}
		

        $this->assign('menu_navbar','账户管理  -- <a href="/admins/account_info.html">添加管理员</a>'.' -- <a href="/admins/account_add.html">添加管理员</a>');
		$this->tpl_full_path('/admins/account/add');
	}
	
	




}
?>
